"""
Various mesh loaders. Each function receives a filename which points to 
a file in some format and returns a Mesh object.
"""
__all__=["loadoff"]

from graphicobject import *
from mesh import *

def loadoff (filename,invertCirculations=False):
    """Reads an off file and returns a corresponding Mesh object.
    @param filename: the name of a readable file in off format.
    @param invertCirculations: tells whether to take face circulations in
    inverse order than the one in the file.
    @return: a mesh object.
    """
    f = open(filename)
    # Read first line
    s = f.readline().strip()
    assert(s=="OFF")
    # Read second line
    nvtx,nface,ntet=map(lambda i: int(i), f.readline().split())
    assert(ntet==0)
    # Read vertices
    vtx = []
    for ivtx in range(nvtx):
        x,y,z = map(lambda i: float(i), f.readline().split())
        vtx.append((x,y,z))
    # Read faces
    face = []
    for iface in range(nface):
        vlist = map(lambda i: int(i), f.readline().split())
        assert (len(vlist) == vlist[0]+1)
        face.append(vlist[1:])
        if invertCirculations:
            face[-1].reverse()
    return Mesh(face,vtx)

if __name__=="__main__":
    loadoff("ape.off")

    
